package cn.stylefeng.guns.sys.modular.system.service;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.sys.modular.rest.entity.RestUser;
import cn.stylefeng.guns.sys.modular.system.db4.mapper.TeamMapper;
import cn.stylefeng.guns.sys.modular.system.entity.BtyBrand;
import cn.stylefeng.guns.sys.modular.system.mapper.BtyBrandMapper;
import cn.stylefeng.guns.sys.modular.system.mapper.BtyGufenzhuanrangMapper;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 档案管理表 服务实现类
 * </p>
 *
 * @author liuy
 * @since
 */
@Service
public class BtyBrandService extends ServiceImpl<BtyBrandMapper, BtyBrand> {

    @Resource
    private BtyGufenzhuanrangService gufenzhuanrangService;
    @Resource
    private TeamMapper teamMapper;

    /**
     * 获取档案管理列表
     *
     * @author fengshuonan
     * @Date 2018/12/23 6:05 PM
     */

    public Page<Map<String, Object>> list(String condition,String dm) {
        Page page = LayuiPageFactory.defaultPage();
        return this.baseMapper.list(page, condition,dm);
    }

    public Page<Map<String, Object>> listYaoma(String condition,String dm,String flag) {
        Page page = LayuiPageFactory.defaultPage();
        return this.baseMapper.listYaoma(page, condition,dm,flag);
    }
    public Page<Map<String, Object>> listAllYaoma(String dm) {
        Page page = LayuiPageFactory.defaultPage();
        return this.baseMapper.listAllYaoma(page,dm);
    }

    public List<Map<String, Object>> listSearchExport(String dm) {
        return this.baseMapper.listSearchExport(dm);
    }

    public List<BtyBrand> kjList() {
        return this.baseMapper.kjList();
    }

    public List<BtyBrand> dmList() {
        return this.baseMapper.dmList();
    }
    public List<BtyBrand> dmLists() {
        return this.baseMapper.dmLists();
    }

    /**
     * 导入财务表excel
     */
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public Boolean batchImport(String fileName, MultipartFile file) throws Exception{

        boolean notNull = false;

        if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {
            throw new Exception("上传文件格式不正确");
        }
        boolean isExcel2003 = true;
        if (fileName.matches("^.+\\.(?i)(xlsx)$")) {
            isExcel2003 = false;
        }

        InputStream is = file.getInputStream();
        Workbook wb = null;
        if (isExcel2003) {
            wb = new HSSFWorkbook(is);
        } else {
            wb = new XSSFWorkbook(is);
        }

        List<Map<Object, Object>> list = new ArrayList<Map<Object, Object>>();

        //excel中多个sheet表，获取所有sheet表的数量
        int sheets = wb.getNumberOfSheets();
        //循环sheet
        for (int i = 0; i < sheets; i++) {
            Sheet sheet = wb.getSheetAt(i);
            if (sheet != null) {
                notNull = true;
            }

            //循环行
            for (int r = 1; r <= sheet.getLastRowNum(); r++) {
                Row row = sheet.getRow(r);
                if (row == null) {
                    continue;
                }

                Map<Object, Object> map = new HashMap<>();

                //循环单元格cell
                for (int cellNum = 0; cellNum <= row.getLastCellNum(); cellNum++) {
                    Cell cell = row.getCell(cellNum);
                    //把单元格的内容转换为字符串
                    if (cell != null) {

                        cell.setCellType(CellType.STRING);
                        map.put(cellNum, cell.getStringCellValue());

                    }
                }

                list.add(map);
            }
        }


        //循环取出的数据
        for (Map map : list) {

            String cwm = map.get(0) == null ? "" : map.get(0).toString();//财务码
            String dm = map.get(1) == null ? "" : map.get(1).toString();//店面

            BtyBrand btyBrand = new BtyBrand();
            btyBrand.setCwm(cwm);
            btyBrand.setDm(dm);

            //判断该财务码是否重复
            int count = this.baseMapper.countCwm(btyBrand);

            if (count == 0){
                //新增数据
                this.baseMapper.add(btyBrand);
            }
        }

        return notNull;
    }
    /**
     * 查询店名列表
     * @return
     */
    public List<Map> dmListGf(String account) {

//        List<Map> mapList = new ArrayList<>();
//
//        if (ToolUtil.isNotEmpty(account)){
//
//            //获取用户角色名称
//            List<String> list = gufenzhuanrangService.roleList(account);
//
//            if (list.contains("督导")){
//
//                mapList = teamMapper.getDdList(account);
//
//            }else if (list.contains("营运经理")){
//
//                mapList = teamMapper.getYyList(account);
//
//            }else if (list.contains("总部股份审核") || list.contains("财务股份审核")){
//
//                mapList = this.baseMapper.dmListGf();
//
//            }
//        }

        List<Map> mapList = this.baseMapper.dmListGf();

        return mapList;
    }
    public static List<Map> ymList = new ArrayList<>();
    public String getYmList(String shopName) {
        if (ToolUtil.isEmpty(ymList)){
            ymList = this.baseMapper.getYmList();
        }
        String yaoma = "";
        Map map = new HashMap<>();

        if (ToolUtil.isNotEmpty(ymList)){
            for (Map listMap : ymList){
                if (String.valueOf(listMap.get("dm")).equalsIgnoreCase(shopName)){
                    map.put("yaoma", listMap.get("yaoma"));
                }
            }

        }
        if (ToolUtil.isNotEmpty(map)){
            yaoma = map.get("yaoma").toString();
        }else {
            //重新取一遍值
            yaoma = ymList(shopName);
        }
        return yaoma;
    }

    public String ymList(String shopName){
        ymList = this.baseMapper.getYmList();
        String yaoma = "";
        Map map = new HashMap<>();

        if (ToolUtil.isNotEmpty(ymList)){
            for (Map listMap : ymList){
                if (String.valueOf(listMap.get("dm")).equalsIgnoreCase(shopName)){
                    map.put("yaoma", listMap.get("yaoma"));
                }
            }
        }
        if (ToolUtil.isNotEmpty(map)){
            yaoma = map.get("yaoma").toString();
        }
        return yaoma;
    }
    public Page<Map<String, Object>> listCwm(String cwm,String searchAll) {
        Page page = LayuiPageFactory.defaultPage();
        if(cwm==""&&searchAll!=null){
            cwm="";
        }else if(cwm!=null&&searchAll!=null||cwm==null&&searchAll==null){
            cwm="0";
        }
        return this.baseMapper.listCwm(page,cwm,searchAll);
    }

    public List<Map<String, Object>> listCwmAll() {
        return this.baseMapper.listCwmAll();
    }
    public int countCwm(BtyBrand btyBrand){
        return this.baseMapper.countCwm(btyBrand);
    }

    public int getMaxCwm(){
        return this.baseMapper.getMaxCwm();
    }
}
